var form;
var $;
var laydate;
var upload;
layui.use(['form','jquery','layer','laydate','upload'], function() {
    form = layui.form;
    $ = layui.jquery;
    laydate = layui.laydate;
    upload = layui.upload;

    /*选择出生日期*/
    laydate.render({
        elem: "#birthDate",
        type: "date",
        trigger: "click"
    });


    var sexx = "";
    form.on("radio(sex)",function (data) {
        sexx = data.value;
    });
    var enabledd ="";
    form.on("radio(enabled)",function (data) {
        enabledd = data.value;
    });
    /*添加用户*/
    form.on('submit(sub)', function (data) {
        if(sexx==""){
            var sex = "男";
        }else{
            var sex = "女";
        };
        if(enabledd==""){
            var enabled = "1";
        }else{
            var enabled = "2";
        };
        var registerDate = new Date();
        registerDate = getTime(registerDate);
        var field = data.field;
        var json = {
            'trueName':field.trueName,
            'sex':sex,
            'account':field.account,
            'acatarUrl':mainImage,
            'email':field.email,
            'password':field.password,
            'remark':field.remark,
            'enabled':enabled,
            'phone':field.phone,
            'idCard':field.idCard,
            'birthDate':field.birthDate,
            'freestyle':field.freestyle,
            'deptId':field.deptId,
            'registerDate':registerDate,
            'status':1,
            'wages':field.wages,
        }
        console.log(json);
        $.ajax({
            type:"post",
            dataType:"json",
            data:json,
            url:"/newJurisdiction/addUser",
            success:function (data) {
                if(data.data){
                    alert("添加成功");
                    parent.$("#Jump").val('后台人员列表');
                    parent.getQuick();
                    return;
                }else{
                    alert("添加失败");
                }
            }
        });

    });
    /*工资验证*/
    form.verify({
        price: function (value, item) {
            if (/^-[0-9]*[1-9][0-9]*$/.test(value)) {
                return '工资不能为负数';
            }
        }
    });
    /*上传头像*/
    var mainImage;
    var uploadInst = upload.render({
        elem: "#acatarUrl",
        url: "/newJurisdiction/uploadFile",
        multiple: true,
        field: "file",
        size:"2M",
        accept: "images",
        before: function (obj) {
            $('#images').show();
        },
        done: function (res, index, upload) {
            mainImage = res.data.path;
            $("#acatarUrl").attr("src","http://"+mainImage);
        },
        error: function () {
            var demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function () {
                uploadInst.upload();
            });
        }
    });
});
/*获取当前时间*/
function getTime(date){
    var year = date.getFullYear(); //获取完整的年份(4位,1970-????)
    var month = date.getMonth()+1; //获取当前月份(0-11,0代表1月)
    var today = date.getDate(); //获取当前日(1-31)
    var hour = date.getHours(); //获取当前小时数(0-23)
    var minute = date.getMinutes(); //获取当前分钟数(0-59)
    var second = date.getSeconds(); //获取当前秒数(0-59)
    var nowTime;
    nowTime = year+'-'+fillZero(month)+'-'+fillZero(today)+' '+fillZero(hour)+':'+fillZero(minute)+':'+fillZero(second);
    return nowTime;
};
/*时间补零*/
function fillZero(str){
    var realNum;
    if(str<10){
        realNum	= '0' + str;
    }else{
        realNum	= str;
    }
    return realNum;
};
/*返回开始*/
goBack = function goBack() {
    //通过页面查找iframe
    parent.$('#Jump').val('首页');
    parent.getQuick();
    return;
}
/*刷新*/
Refresh = function Refresh() {
    location.reload();
};